<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Graph API</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Graph API </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="gapi_root_intro"></a>
Introduction</h1>
<p>OpenCV Graph API (or G-API) is a new OpenCV module targeted to make regular image processing fast and portable. These two goals are achieved by introducing a new graph-based model of execution.</p>
<p>G-API is a special module in OpenCV &ndash; in contrast with the majority of other main modules, this one acts as a framework rather than some specific CV algorithm. G-API provides means to define CV operations, construct graphs (in form of expressions) using it, and finally implement and run the operations for a particular backend.</p>
<dl class="section note"><dt>Note</dt><dd>G-API is a new module and now is in active development. It's API is volatile at the moment and there may be minor but compatibility-breaking changes in the future.</dd></dl>
<h1>Contents</h1>
<p>G-API documentation is organized into the following chapters:</p>
<ul>
<li><p class="startli"><a class="el" href="../../d6/dc9/gapi_purposes.html">Why Graph API?</a></p>
<p class="startli">The motivation behind G-API and its goals.</p>
</li>
<li><p class="startli"><a class="el" href="../../de/d4d/gapi_hld.html">High-level design overview</a></p>
<p class="startli">General overview of G-API architecture and its major internal components.</p>
</li>
<li><p class="startli"><a class="el" href="../../d0/d25/gapi_kernel_api.html">Kernel API</a></p>
<p class="startli">Learn how to introduce new operations in G-API and implement it for various backends.</p>
</li>
<li><p class="startli"><a class="el" href="../../d4/ddd/gapi_impl.html">Implementation details</a></p>
<p class="startli">Low-level implementation details of G-API, for those who want to contribute.</p>
</li>
<li>API Reference: functions and classes<ul>
<li><p class="startli"><a class="el" href="../../df/d1f/group__gapi__core.html">G-API Core functionality</a></p>
<p class="startli">Core G-API operations - arithmetic, boolean, and other matrix operations;</p>
</li>
<li><p class="startli"><a class="el" href="../../d2/d00/group__gapi__imgproc.html">G-API Image processing functionality</a></p>
<p class="startli">Image processing functions: color space conversions, various filters, etc.</p>
</li>
</ul>
</li>
</ul>
<h1><a class="anchor" id="gapi_example"></a>
API Example</h1>
<p>A very basic example of G-API pipeline is shown below:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d7/d20/gapi_8hpp.html">opencv2/gapi.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/df9/gapi_2include_2opencv2_2gapi_2core_8hpp.html">opencv2/gapi/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/d75/gapi_2include_2opencv2_2gapi_2imgproc_8hpp.html">opencv2/gapi/imgproc.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv::VideoCapture</a> cap;</div><div class="line">    <span class="keywordflow">if</span> (argc &gt; 1) cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(argv[1]);</div><div class="line">    <span class="keywordflow">else</span> cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(0);</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>(cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585">isOpened</a>());</div><div class="line"></div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> in;</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> vga      = <a class="code" href="../../d6/d91/group__gapi__transform.html#gad0ae87ad8b909c3c11a07b007837be8b">cv::gapi::resize</a>(in, <a class="code" href="../../d6/d50/classcv_1_1Size__.html">cv::Size</a>(), 0.5, 0.5);</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> gray     = <a class="code" href="../../dc/d38/group__gapi__colorconvert.html#ga3e8fd8197ab16811caf9b31cb1e08a05">cv::gapi::BGR2Gray</a>(vga);</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> blurred  = <a class="code" href="../../da/dc5/group__gapi__filters.html#gae50f1db53f567b9c846851c6e0d81849">cv::gapi::blur</a>(gray, <a class="code" href="../../d6/d50/classcv_1_1Size__.html">cv::Size</a>(5,5));</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> edges    = <a class="code" href="../../d5/def/group__gapi__feature.html#gad93b9f85e6abecc152d05d3e75bac37a">cv::gapi::Canny</a>(blurred, 32, 128, 3);</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> b,g,r;</div><div class="line">    std::tie(b,g,r)   = <a class="code" href="../../d6/d91/group__gapi__transform.html#gabfeb794ff284b18121b2b755a5391892">cv::gapi::split3</a>(vga);</div><div class="line">    <a class="code" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> out      = <a class="code" href="../../d6/d91/group__gapi__transform.html#gaf63e894270a088695e6ec97d9a903971">cv::gapi::merge3</a>(b, g | edges, r);</div><div class="line">    <a class="code" href="../../d9/dfe/classcv_1_1GComputation.html">cv::GComputation</a> ac(in, out);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> input_frame;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> output_frame;</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>(cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a473055e77dd7faa4d26d686226b292c1">read</a>(input_frame));</div><div class="line">    <span class="keywordflow">do</span></div><div class="line">    {</div><div class="line">        ac.apply(input_frame, output_frame);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">cv::imshow</a>(<span class="stringliteral">&quot;output&quot;</span>, output_frame);</div><div class="line">    } <span class="keywordflow">while</span> (cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a473055e77dd7faa4d26d686226b292c1">read</a>(input_frame) &amp;&amp; <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv::waitKey</a>(30) &lt; 0);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><p>G-API is a separate OpenCV module so its header files have to be included explicitly. The first four lines of <code>main()</code> create and initialize OpenCV's standard video capture object, which fetches video frames from either an attached camera or a specified file.</p>
<p>G-API pipeline is constructed next. In fact, it is a series of G-API operation calls on <a class="el" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> data. The important aspect of G-API is that this code block is just a declaration of actions, but not the actions themselves. No processing happens at this point, G-API only tracks which operations form pipeline and how it is connected. G-API <em>Data objects</em> (here it is <a class="el" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a>) are used to connect operations each other. <code>in</code> is an <em>empty</em> <a class="el" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> signalling that it is a beginning of computation.</p>
<p>After G-API code is written, it is captured into a call graph with instantiation of <a class="el" href="../../d9/dfe/classcv_1_1GComputation.html" title="GComputation class represents a captured computation graph. GComputation objects form boundaries for ...">cv::GComputation</a> object. This object takes input/output data references (in this example, <code>in</code> and <code>out</code> <a class="el" href="../../df/daa/classcv_1_1GMat.html">cv::GMat</a> objects, respectively) as parameters and reconstructs the call graph based on all the data flow between <code>in</code> and <code>out</code>.</p>
<p><a class="el" href="../../d9/dfe/classcv_1_1GComputation.html" title="GComputation class represents a captured computation graph. GComputation objects form boundaries for ...">cv::GComputation</a> is a thin object in sense that it just captures which operations form up a computation. However, it can be used to execute computations &ndash; in the following processing loop, every captured frame (a <a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">cv::Mat</a> <code>input_frame</code>) is passed to <a class="el" href="../../d9/dfe/classcv_1_1GComputation.html#aa25bc73f7cba9bb2f9992edaa248f868" title="Compile graph on-the-fly and immediately execute it on the inputs data vectors. ">cv::GComputation::apply()</a>.</p>
<div class="image">
<img src="../../demo.jpg" alt="demo.jpg"/>
<div class="caption">
Example pipeline running on sample video 'vtest.avi'</div></div>
<p> <a class="el" href="../../d9/dfe/classcv_1_1GComputation.html#aa25bc73f7cba9bb2f9992edaa248f868" title="Compile graph on-the-fly and immediately execute it on the inputs data vectors. ">cv::GComputation::apply()</a> is a polimorphic method which accepts a variadic number of arguments. Since this computation is defined on one input, one output, a special overload of <a class="el" href="../../d9/dfe/classcv_1_1GComputation.html#aa25bc73f7cba9bb2f9992edaa248f868" title="Compile graph on-the-fly and immediately execute it on the inputs data vectors. ">cv::GComputation::apply()</a> is used to pass input data and get output data.</p>
<p>Internally, <a class="el" href="../../d9/dfe/classcv_1_1GComputation.html#aa25bc73f7cba9bb2f9992edaa248f868" title="Compile graph on-the-fly and immediately execute it on the inputs data vectors. ">cv::GComputation::apply()</a> compiles the captured graph for the given input parameters and executes the compiled graph on data immediately.</p>
<p>There is a number important concepts can be outlines with this example:</p><ul>
<li>Graph declaration and graph execution are distinct steps;</li>
<li>Graph is built implicitly from a sequence of G-API expressions;</li>
<li>G-API supports function-like calls &ndash; e.g. <a class="el" href="../../d6/d91/group__gapi__transform.html#gad0ae87ad8b909c3c11a07b007837be8b" title="Resizes an image. ">cv::gapi::resize()</a>, and operators, e.g <a class="el" href="../../d1/d10/classcv_1_1MatExpr.html#a8938333ac6cc65c2cc4a21071b22df56">operator|()</a> which is used to compute bitwise OR;</li>
<li>G-API syntax aims to look pure: every operation call within a graph yields a new result, thus forming a directed acyclic graph (DAG);</li>
<li>Graph declaration is not bound to any data &ndash; real data objects (<a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">cv::Mat</a>) come into picture after the graph is already declared.</li>
</ul>
<p>See <a class="el" href="../../df/d7e/tutorial_table_of_content_gapi.html">tutorials and porting examples</a> to learn more on various G-API features and concepts. </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:33 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
